package com.xingongchang.baolebei.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.external.androidquery.callback.AjaxStatus;
import com.google.gson.JsonObject;
import com.insthub.BeeFramework.activity.BaseActivity;
import com.insthub.BeeFramework.activity.WebViewActivity;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.insthub.BeeFramework.view.MyDialog;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.umeng.message.UmengRegistrar;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;
import com.xingongchang.baolebei.BaolebeiApp;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.model.ConfigModel;
import com.xingongchang.baolebei.model.LoginModel;
import com.xingongchang.baolebei.model.LogoutModel;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.protocol.SESSION;

public class SettingActivity extends BaseActivity implements OnClickListener,BusinessResponse {
	
	private TextView title;
	private ImageView back;
	
	private ImageView settingPush;
	private ImageView setting_update;
	
	private LinearLayout type1; //智能模式
	private LinearLayout type2; //高质量模式
	private LinearLayout type3; //普通模式
	
	private ImageView invoice1;
	private ImageView invoice2;
	private ImageView invoice3;
	
	private TextView mobile;
	private TextView version_name;
	private LinearLayout official_web;
	private LinearLayout aboutApp;
    private LinearLayout settingMobileLayout;
    private LinearLayout about;
    private LinearLayout settingSupport;

	private Button exitLogin;
	LoginModel loginModel;
	LogoutModel logoutModel;
	private SharedPreferences shared;
	private SharedPreferences.Editor editor;
	
	private MyDialog mDialog;
	
	private boolean pushSwitch = true;
	PushAgent mPushAgent ;

	@Override
	protected void onCreate(Bundle savedInstanceState)
    {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		mPushAgent= PushAgent.getInstance(SettingActivity.this);
		shared = getSharedPreferences("userInfo", 0); 
		editor = shared.edit();
	
		
		
        if (null == ConfigModel.getInstance())
        {
            ConfigModel configModel = new ConfigModel(this);
            configModel.addResponseListener(this);
            ConfigModel.getInstance().getConfig();
        }
        else if (null == ConfigModel.getInstance().config)
        {
            ConfigModel.getInstance().addResponseListener(this);
            ConfigModel.getInstance().getConfig();
        }

		
		title = (TextView) findViewById(R.id.top_view_text);
        Resources resource = (Resources) getBaseContext().getResources();
        String set=resource.getString(R.string.setting);
		title.setText(set);
		back = (ImageView) findViewById(R.id.top_view_back);
		back.setOnClickListener(this);
		
		settingPush = (ImageView) findViewById(R.id.setting_push);
		setting_update=(ImageView) findViewById(R.id.setting_update);
		
		type1 = (LinearLayout) findViewById(R.id.setting_type1);
		type2 = (LinearLayout) findViewById(R.id.setting_type2);
		type3 = (LinearLayout) findViewById(R.id.setting_type3);
		
		invoice1 = (ImageView) findViewById(R.id.setting_invoice1);
		invoice2 = (ImageView) findViewById(R.id.setting_invoice2);
		invoice3 = (ImageView) findViewById(R.id.setting_invoice3);
		version_name = (TextView) findViewById(R.id.version_name);
        settingMobileLayout = (LinearLayout)findViewById(R.id.setting_mobile_layout);
        settingMobileLayout.setOnClickListener(this);
        
        settingSupport = (LinearLayout) findViewById(R.id.setting_support);
        settingSupport.setOnClickListener(this);

		mobile = (TextView) findViewById(R.id.setting_mobile);

        if (null != ConfigModel.getInstance().config &&
                null != ConfigModel.getInstance().config.service_phone)
        {
            mobile.setText(ConfigModel.getInstance().config.service_phone);
        }

		official_web = (LinearLayout) findViewById(R.id.setting_official_web);
		aboutApp = (LinearLayout) findViewById(R.id.setting_aboutApp);
		about = (LinearLayout) findViewById(R.id.setting_about);
		exitLogin = (Button) findViewById(R.id.setting_exitLogin);
		
		type1.setOnClickListener(this);
		type2.setOnClickListener(this);
		type3.setOnClickListener(this);
		
		official_web.setOnClickListener(this);
		aboutApp.setOnClickListener(this);
		about.setOnClickListener(this);
		exitLogin.setOnClickListener(this);
		
		String imageType = shared.getString("imageType", "mind");
		if(imageType.equals("high")) {
			invoice1.setVisibility(View.GONE);
			invoice2.setVisibility(View.VISIBLE);
			invoice3.setVisibility(View.GONE);
		} else if(imageType.equals("low")) {
			invoice1.setVisibility(View.GONE);
			invoice2.setVisibility(View.GONE);
			invoice3.setVisibility(View.VISIBLE);
		} else {
			invoice1.setVisibility(View.VISIBLE);
			invoice2.setVisibility(View.GONE);
			invoice3.setVisibility(View.GONE);
		}
		version_name.setText("当前版本:"+BaolebeiApp.version_name);
		String user_id = shared.getString("uid", "");
		if (user_id.equals("")) {
			exitLogin.setVisibility(View.GONE);
		} else {
			exitLogin.setVisibility(View.VISIBLE);
		}
        //推送消息
		settingPush.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pushSwitch) {
                	//关
                	pushSwitch = false;
                	settingPush.setImageResource(R.drawable.off);
//                	关闭推送
                	mPushAgent.disable();
                } else {
                	//开
                	pushSwitch = true;
                	settingPush.setImageResource(R.drawable.on);
                	//开启消息推送
            		mPushAgent.enable();
//            		String device_token = UmengRegistrar.getRegistrationId(SettingActivity.this);
//            		System.out.println(device_token);
                }
            }
        });
		//开始更新
		setting_update.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				//Toast.makeText(getApplicationContext(), "开始更新", 0).show();
				UmengUpdateAgent.setUpdateOnlyWifi(false);
				UmengUpdateAgent.setUpdateAutoPopup(false);
				UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {

					@Override
					public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
						// TODO Auto-generated method stub
						   switch (updateStatus) {
					        case UpdateStatus.Yes: // has update
					            UmengUpdateAgent.showUpdateDialog(SettingActivity.this, updateInfo);
					            break;
					        case UpdateStatus.No: // has no update
					            Toast.makeText(SettingActivity.this, "没有更新", Toast.LENGTH_SHORT).show();
					            break;
					        case UpdateStatus.NoneWifi: // none wifi
					            Toast.makeText(SettingActivity.this, "没有wifi连接， 只在wifi下更新", Toast.LENGTH_SHORT).show();
					            break;
					        case UpdateStatus.Timeout: // time out
					            Toast.makeText(SettingActivity.this, "超时", Toast.LENGTH_SHORT).show();
					            break;
					        }
					}
				});
				UmengUpdateAgent.update(SettingActivity.this);
			}
		});
	}

	@Override
	public void onClick(View v) {		
		switch(v.getId()) {
		case R.id.top_view_back:
			finish();
			break;
		case R.id.setting_type1:
			invoice1.setVisibility(View.VISIBLE);
			invoice2.setVisibility(View.GONE);
			invoice3.setVisibility(View.GONE);
			editor.putString("imageType", "mind");
			editor.commit();
			break;
		case R.id.setting_type2:
			invoice1.setVisibility(View.GONE);
			invoice2.setVisibility(View.VISIBLE);
			invoice3.setVisibility(View.GONE);
			editor.putString("imageType", "high");
			editor.commit();
			break;
		case R.id.setting_type3:
			invoice1.setVisibility(View.GONE);
			invoice2.setVisibility(View.GONE);
			invoice3.setVisibility(View.VISIBLE);
			editor.putString("imageType", "low");
			editor.commit();
			break;
		case R.id.setting_official_web:
        {
            Intent it = new Intent(SettingActivity.this, WebViewActivity.class);
            it.putExtra(WebViewActivity.WEBURL, ConfigModel.getInstance().config.site_url);
            Resources resource = (Resources) getBaseContext().getResources();
            String off=resource.getString(R.string.setting_website);
            it.putExtra(WebViewActivity.WEBTITLE, off);
            startActivity(it);
            break;
        }
		case R.id.setting_aboutApp:
        {
            Intent it = new Intent(SettingActivity.this, WebViewActivity.class);
            it.putExtra(WebViewActivity.WEBURL, ConfigModel.getInstance().config.site_url);
            Resources resource = (Resources) getBaseContext().getResources();
            String tech=resource.getString(R.string.setting_tech);
            it.putExtra(WebViewActivity.WEBTITLE, tech);
            startActivity(it);
            break;
        }
        case R.id.setting_mobile_layout:
        {
            Resources resource = (Resources) getBaseContext().getResources();
            String call=resource.getString(R.string.call_or_not);
            mDialog = new MyDialog(SettingActivity.this, call, ConfigModel.getInstance().config.service_phone);
            mDialog.show();
            mDialog.positive.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {					
					mDialog.dismiss();
					Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ConfigModel.getInstance().config.service_phone));
		            startActivity(intent);
				}
			});
            mDialog.negative.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {					
					mDialog.dismiss();
				}
			});
            
           break;
        }
        
        case R.id.setting_about:
        	Intent it = new Intent(SettingActivity.this, WebViewActivity.class);
            it.putExtra(WebViewActivity.WEBURL, ConfigModel.getInstance().config.site_url);
            it.putExtra(WebViewActivity.WEBTITLE, "Geek-Zoo介绍");
            startActivity(it);
        	break;
        case R.id.setting_support:
        	Intent intent = new Intent(SettingActivity.this, WebViewActivity.class);
            intent.putExtra(WebViewActivity.WEBURL, "http://ecmobile.me/copyright.html");
            intent.putExtra(WebViewActivity.WEBTITLE, "版权声明");
            startActivity(intent);
        	break;
		case R.id.setting_exitLogin:
            Resources resource = (Resources) getBaseContext().getResources();
			String exit=resource.getString(R.string.exit);
            String exiten=resource.getString(R.string.ensure_exit);
			mDialog = new MyDialog(SettingActivity.this, exit, exiten);
            mDialog.show();
            mDialog.positive.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {					
					mDialog.dismiss();
					logoutModel = new LogoutModel(SettingActivity.this);
					logoutModel.logout(shared.getString("uid", "0"),shared.getString("sid", ""));

					editor.putString("uid", "");
		            editor.putString("sid", "");
		            editor.commit();
		            SESSION.getInstance().uid = shared.getString("uid", "");
		            SESSION.getInstance().sid = shared.getString("sid", "");
		            Intent intent = new Intent(SettingActivity.this, BaolebeiActivity.class);
		            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK);
		            startActivity(intent);
		            finish();
				}
			});
            mDialog.negative.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {					
					mDialog.dismiss();
				}
			});
			
			break;
		}
		
	}

    @Override

    public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status) throws JSONException
    {
        if (url.endsWith(ProtocolConst.CONFIG))
        {
            if (null != ConfigModel.getInstance().config &&
                    null != ConfigModel.getInstance().config.service_phone)
            {
                mobile.setText(ConfigModel.getInstance().config.service_phone);
            }
        }
        if(url.endsWith(ProtocolConst.CANAEL)){
        	Intent intent = new Intent();
			intent.putExtra("uid",SESSION.getInstance().uid);
			intent.putExtra("sid",SESSION.getInstance().sid);
			setResult(Activity.RESULT_OK, intent);
			finish();
        }
    }

    @Override
    protected void onDestroy()
    {
        ConfigModel.getInstance().removeResponseListener(this);
        super.onDestroy();
    }

}
